package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/21 21:55
 * http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
 */
@ResponseBody
@Controller
@RequestMapping("/admin")
@CrossOrigin
public class UserManager {
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsAccountTraceService dtsAccountTraceService;
    @Autowired
    private DtsAddressService dtsAddressService;
    @Autowired
    private DtsUserAccountService dtsUserAccountService;
    @Autowired
    private DtsCollectService dtsCollectService;
    @Autowired
    private DtsFootprintService dtsFootprintService;
    @Autowired
    private DtsSearchHistoryService dtsSearchHistoryService;
    @Autowired
    private DtsFeedbackService dtsFeedbackService;

    //会员管理========================================================================
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "查询")
    @RequiresPermissions("admin:user:list")
    @GetMapping("/user/list")
    public Object list(String username, String phone, Integer page, Integer limit, String sort, String order) {


        Page<DtsUser> dtsUserList = dtsUserService.findByPage(username, phone, page, limit, sort, order);
        long total = dtsUserList.getTotal();
        List<DtsUser> records = dtsUserList.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "非代理")
    @RequiresPermissions("admin:user:approveAgency")
    @PostMapping("/user/approveAgency")
    public Object approveAgency(@RequestBody DtsUser dtsUser) {

        return ResponseUtil.ok();
    }

    /**
     * - 接口地址 : http://localhost:8083/admin/user/detailApprove?id=71
     * <p>
     * - 下行数据 :
     * {
     * "errno": 0,
     * "data": {
     * "id": 5,
     * "userId": 71,
     * "remainAmount": 0,
     * "totalAmount": 0,
     * "createTime": "2019-10-28 11:21:40",
     * "modifyTime": "2019-12-15 12:32:14",
     * "settlementRate": 8,
     * "status": 1
     * },
     * "errmsg": "成功"
     * }
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "代理佣金")
    @RequiresPermissions("admin:user:approveAgency")
    @GetMapping("/user/detailApprove")
    public Object detailApprove(Integer id) {
        DtsUserAccount dtsUserAccount = dtsUserAccountService.selectUserById(id);

        return ResponseUtil.ok(dtsUserAccount);

    }

    //佣金管理===========================================
    @GetMapping("/brokerage/list")
    public Object brokerAge(String username, String phone, Integer page, Integer limit, String sort, String order,Integer[] statusArray) {

        Page<DtsAccountTrace> dtsAccountTracePage = dtsAccountTraceService.selectList(username, phone, page, limit, sort, order,statusArray);
        long total = dtsAccountTracePage.getTotal();
        List<DtsAccountTrace> records = dtsAccountTracePage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("traceList", records);
        return ResponseUtil.ok(data);
    }

    // http://localhost:8080/admin/brokerage/approve?id=14&status=2
    @PostMapping("brokerage/approve")
    public Object approve(@RequestBody DtsAccountTrace dtsAccountTrace) {
        dtsAccountTraceService.update(dtsAccountTrace);
        return ResponseUtil.ok();
    }
//地址=================================================================

    /**
     * - 接口地址 : http://localhost:8083/admin/address/list?page=1&limit=20&sort=add_time&order=desc
     * <p>
     * - 下行数据 :
     */
    @GetMapping("/address/list")
    public Object addressList(String name, Integer userId, Integer page, Integer limit, String sort, String order) {
        Page<DtsAddress> dtsAddressPage = dtsAddressService.selectPage(name, userId, page, limit, sort, order);
        long total = dtsAddressPage.getTotal();
        List<DtsAddress> records = dtsAddressPage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/admin/order")
    public Object orderList(Integer page, Integer limit, String sort, String order) {

        return ResponseUtil.ok();
    }

    //接口地址 : http://localhost:8083/admin/collect/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"用户管理", "会员收藏"}, button = "查询")
    @RequiresPermissions("admin:collect:list")
    @GetMapping("/collect/list")
    public Object collectList(Integer valueId, Integer userId, Integer page, Integer limit, String sort, String order) {
        Page<DtsCollect> dtsCollectPage = dtsCollectService.selectPage(page, limit, sort, order, userId, valueId);
        List<DtsCollect> records = dtsCollectPage.getRecords();
        long total = dtsCollectPage.getTotal();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    //接口地址 : http://localhost:8083/admin/footprint/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("/footprint/list")
    public Object footPrintList(Integer goodsId, Integer userId, Integer page, Integer limit, String sort, String order) {
        Page<DtsFootprint> dtsFootprintPage = dtsFootprintService.selectPage(page, limit, sort, order, userId, goodsId);
        long total = dtsFootprintPage.getTotal();
        List<DtsFootprint> records = dtsFootprintPage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    //===================================================================================
    //http://localhost:8080/admin/history/list?page=1&limit=20&userId=ss&keyword=ssss&sort=add_time&order=desc'
    @GetMapping("/history/list")
    public Object historyList(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        Page<DtsSearchHistory> dtsSearchHistoryPage = dtsSearchHistoryService.selectPage(page, limit, sort, order, userId, keyword);
        List<DtsSearchHistory> records = dtsSearchHistoryPage.getRecords();
        long total = dtsSearchHistoryPage.getTotal();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    // http://localhost:8080/admin/feedback/list?page=1&limit=20&username=qq&sort=add_time&order=desc&id=qq
    @GetMapping("/feedback/list")
    public Object feedbackList(Integer page, Integer limit, String sort, String order, String username, Integer id) {
        Page<DtsFeedback> dtsFeedbackPage = dtsFeedbackService.selectPage(page, limit, sort, order, username, id);
        long total = dtsFeedbackPage.getTotal();
        List<DtsFeedback> records = dtsFeedbackPage.getRecords();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }
}
